<template>
  <div>


    <el-card class="box-card margin_s">
      <div>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-statistic title="最新版本" v-model="lastVersion.version">
              <template slot="formatter">
                {{ lastVersion.version }}
              </template>
            </el-statistic>
          </el-col>
          <el-col :span="6">
            <el-statistic title="可以跳过" v-model="lastVersion.isJump">
              <template slot="formatter">
              </template>
            </el-statistic>
          </el-col>
          <el-col :span="6">
            <el-statistic title="是否启用" v-model="lastVersion.platform">
              <template slot="formatter">
              </template>
            </el-statistic>
          </el-col>
          <el-col :span="6">
            <el-statistic title="更新信息">
              <template slot="formatter">
                {{ lastVersion.updateInfo }}
              </template>
            </el-statistic>
          </el-col>

        </el-row>
      </div>
    </el-card>

    <el-card style="margin: 10px 0 10px 0px;">

      <el-collapse>
        <el-collapse-item title="添加版本信息" name="1">

          <el-form ref="form" :label-position="'left'" :model="form" label-width="120px" style=" width: 280px;">
            <input type="hidden" name="Id" v-model="form.Id" />
            <el-form-item label="版本号">
              <el-input v-model="form.Version"></el-input>
            </el-form-item>
            <el-form-item label="平台">
              <el-select v-model="form.Platform" placeholder="请选择活动区域">
                <el-option label="Windows" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item :inline="true" label="是否可跳过">
              <el-switch v-model="form.IsJump" :active-value="1" :inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item :inline="true" label="是否启用">
              <el-switch v-model="form.IsEnable" :active-value="1" :inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item label="更新内容">
              <el-input type="textarea" v-model="form.UpdateInfo"></el-input>
            </el-form-item>
            <el-upload class="upload-demo" drag :action="UploadUrl" multiple :on-success="handleUploadSuccess">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">立即创建</el-button>
              <!-- <el-button>取消</el-button> -->
            </el-form-item>
          </el-form>
        </el-collapse-item>
      </el-collapse>
    </el-card>

    <el-card class="box-card" style="margin: 10px 0 10px 0px;">
      <!-- <elTables ref="elTables" :tableData="tableList" :columObj="columObj"> </elTables> -->
      <cusTable :listData="tableData" :propList="propList" @selectionChange="selectionChange">
        <!-- 传入插槽，插槽支持多个 -->
        <!-- 第一种 -->
        <template #status="scope">
          <el-button type="text">{{ scope.row.status === '1' ? '是' : '否' }}</el-button>
        </template>
        <!-- 第二种 -->
        <template slot="sex" slot-scope="{ row }">
          <span>{{ row.sex === '1' ? '男' : '女' }}</span>
        </template>
        <template slot="status" slot-scope="scope">
          <span>{{ scope.row.status === '1' ? '是' : '否' }}</span>
        </template>
        <template #handler="scope">
          <div class="handle-btns">
            <!-- <el-button icon="el-icon-edit" size="mini" type="text">编辑</el-button> -->
            <el-button icon="el-icon-delete" size="mini" type="text" @click="DeleteVersion(scope.row)">删除</el-button>
          </div>
        </template>
      </cusTable>
    </el-card>




  </div>
</template>

<script>

import { GetList, GetLastVersion, Delete, Add } from '@/api/versionApi'
import cusTable from '../components/cusTable.vue'
export default {
  name: 'VersionManager',
  components: {
    cusTable
  },
  data() {
    return {
      UploadUrl: "http://81.70.79.122:8070/Upgrade/UploadFiles",
      tableData: [],
      form: {
        Id: 0,
        Version: '',
        Platform: "1",
        IsEnable: 1,
        UpdateFile: "",
        UpdateInfo: "",
        IsJump: 0,
      },
      lastVersion: {
        Id: 0,
        Version: '',
        Platform: "Windows",
        IsEnable: true,
        UpdateFile: "",
        UpdateInfo: "",
        IsJump: false,
      },

      userList: [],
      propList: [
        { prop: 'version', label: '版本号', width: '100' },
        { prop: 'isJump', label: '跳过', minWidth: '100', slotName: 'status' },
        { prop: 'isEnable', label: '启用', minWidth: '100', slotName: 'status' },
        // slotName 需要自定义数据的插槽名
        { prop: 'updateFile', label: '文件路径', width: '100' },
        { prop: 'updateInfo', label: '更新信息', width: '100' },
        // fzCol 复杂表头
        // {
        //   label: '地址',
        //   minWidth: '250',
        //   fzCol: true,
        //   colList: [
        //     {
        //       label: '市/区', minWidth: '100',
        //       fzCol: true,
        //       colList: [
        //         { prop: 'city', label: '市', minWidth: '100' },
        //         { prop: 'qu', label: '区', minWidth: '100' },
        //       ]
        //     },
        //     { prop: 'cun', label: '村', minWidth: '100' },
        //   ]
        // },
        {
          prop: 'createTime',
          label: '创建时间',
          width: '100',
        },
        { label: '操作', minWidth: '120', slotName: 'handler' }
      ]
    }
  },
  getResult(rowVal, zidian, $index) {
    let result
    zidian.forEach(item => {
      if (rowVal !== "" && item.value == rowVal) {
        result = item.name
      }
    });
    return result
  },
  methods: {
    // 工具栏的请求
    async GetTableList() {
      await GetList().then(res => {
        console.log("res:::", res)
        this.tableData = res.body

      })
    },
    async GetLastVersion() {
      await GetLastVersion().then(res => {
        console.log("res:::", res)
        this.lastVersion = res.body

      })
    },
    selectionChange(val) {
      console.log(val)
    },
    handleUploadSuccess(res, file) {
      this.form.UpdateFile = "";
      console.log("res1::::", res, file);
      this.form.UpdateFile = res.body.path;
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    async onSubmit() {
      console.log("onSubmit::::", this.form)
      await Add(this.form).then(res => {
        console.log("res:::", res)
        // this.lastVersion = res.body
        //重新加载List
        this.GetLastVersion()
        this.GetTableList()
      })
    },
    async DeleteVersion(row) {
      console.log("row::::", row);
      await Delete(row.id).then(res => {

        this.GetLastVersion()
        this.GetTableList()
      })
    }
  },
  created() {
    this.GetLastVersion()
    this.GetTableList()

  }
}

</script>

<style>
.margin_s {
  margin: 10px;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 800px;
  margin: 0 auto;
}
</style>